package tw.com.hitevision.whiteboard.android.retrofit;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import tw.com.hitevision.whiteboard.android.retrofit.service.SuggestQueriesAPI;
import tw.com.hitevision.whiteboard.android.retrofit.service.YoutubeAPIService;

public class RetrofitManager {

    private static final String TAG = RetrofitManager.class.getSimpleName();
    private static final String GOOGLE_API_BASE_URL = "https://www.googleapis.com/";
    private static final String OAUTH2_API_BASE_URL = "https://oauth2.googleapis.com";
    private static final String SUGGEST_QUERIES_API_BASE_URL = "http://suggestqueries.google.com";
    public static final int PAGE_SIZE_COUNT = 100;
    public static final String ACCESS_TOKEN = "access_token";
    public static final String Q_ORDER_BY_NAME = "name";
    public static final String Q_ORDER_BY_TIME = "modifiedTime";
    public static final String Q_ORDER_BY_FOLDER = "folder";
    public static final String Q_ORDER_BY_DIVIDER = ",";
    public static final String Q_ORDER_BY_DESC = " desc"; // reverse result
    public static final String Q_ROOT = "'root'";
    public static final String Q_MIMETYPE_KEY = "mimeType";
    public static final String Q_MIMETYPE_GOOGLE_FOLDER_VALUE = "'application/vnd.google-apps.folder'";
    public static final String Q_NAME_KEY = "name";
    public static final String Q_DIVIDER = " , ";
    public static final String Q_NAME_WHITEBOARD_VALUE = "'Whiteboard'";
    public static final String Q_SEARCH_PREFIX = "name contains ";
    public static final String Q_IN_PARENTS = " in parents";
    public static final String Q_TRASHED_FALSE = "trashed=false";
    public static final String F_KIND_ISCOMPLETE_TOKEN_FILES = "kind, incompleteSearch, nextPageToken, files(kind, id, name, mimeType, thumbnailLink, modifiedTime, parents, shared, trashed, webContentLink, webViewLink, size)";
    private String url;
    private static RetrofitManager manager;
//    private static GoogleDriveAPI googleDriveAPI;
    private static YoutubeAPIService youtubeAPIService;
//    private static Oauth2API oauth2API;
    private static SuggestQueriesAPI suggestQueriesAPI;

    public static RetrofitManager getInstance() {
        return RetrofitManager.getInstance(GOOGLE_API_BASE_URL);
    }

    public static RetrofitManager getOauthInstance() {
        return RetrofitManager.getInstance(OAUTH2_API_BASE_URL);
    }

    public static RetrofitManager getSuggestInstance() {
        return RetrofitManager.getInstance(SUGGEST_QUERIES_API_BASE_URL);
    }

    public static RetrofitManager getInstance(String newUrl) {
        if (manager == null)
            return new RetrofitManager(newUrl);
        else {
            if (!manager.getUrl().equals(newUrl)) {
                return new RetrofitManager(newUrl);
            } else {
                return manager;
            }
        }
    }

    private RetrofitManager(String url) {
        this.url = url;
        LoggingInterceptor interceptor = new LoggingInterceptor(message -> Log.d("OkHttp", message));
        interceptor.setLevel(LoggingInterceptor.Level.BODY);
        OkHttpClient.Builder okHttpClient;
        okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(interceptor)
                    .connectTimeout(20, TimeUnit.SECONDS)
                    .writeTimeout(20, TimeUnit.SECONDS)
                    .readTimeout(20, TimeUnit.SECONDS);


        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .client(okHttpClient.build())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

//        oauth2API = retrofit.create(Oauth2API.class);
//        googleDriveAPI = retrofit.create(GoogleDriveAPI.class);
        youtubeAPIService = retrofit.create(YoutubeAPIService.class);
        suggestQueriesAPI = retrofit.create(SuggestQueriesAPI.class);
        manager = this;
    }

//    public GoogleDriveAPI getGoogleDriveAPI() {
//        return googleDriveAPI;
//    }

    public YoutubeAPIService getYoutubeAPIService() {
        return youtubeAPIService;
    }

//    public Oauth2API getOauth2API() {
//        return oauth2API;
//    }
//
    public SuggestQueriesAPI getSuggestQueriesAPI() {
        return suggestQueriesAPI;
    }

    public String getUrl(){
        return url;
    }

}
